//
//  PolicyFileListViewController.m
//  homeland
//
//  Created by SunShine on 13-10-29.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PolicyFileListViewController.h"
#import "PolicyFileCell.h"
#import "PolicyFileViewController.h"

@interface PolicyFileListViewController ()
@property (strong, nonatomic) PolicyFileService *dataService;
@property (strong, nonatomic) CustomPullToRefresh *ptr;
@end

@implementation PolicyFileListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"政策文件";
    self.dataService = [PolicyFileService new];
    _dataService.delegate = self;
    _dataService.unitName = _unitName;
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.tableView delegate:self];
    _ptr.enablePullToBottom = NO;
    [self startLoadingView];
    [_dataService loadDataWithKey:_searchKey];
//    self.searchBar.inputAccessoryView = [self getInputAccessoryView];
//    self.searchBar.delegate = self;
//    if (_searchKey && _searchKey.length > 0) {
//        _searchBar.text = _searchKey;
//    }
    [self showSearchView];
    if (_searchKey && _searchKey.length > 0) {
        self.searchView.textField.text = _searchKey;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Tables
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataService.dataList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EnterprisePolicyFileItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    CGFloat height = [self caculateHeightForString:item.title AndFontSize:14.0 AndWitdh:310.0];
    return MAX(44.0, height+10.0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EnterprisePolicyFileItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    PolicyFileCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"PolicyFileCell" owner:self options:nil] objectAtIndex:0];
    cell.titleTextView.text = item.title;
    CGFloat height = [self caculateHeightForString:item.title AndFontSize:14.0 AndWitdh:310.0];
    cell.titleTextView.frame = CGRectMake(5.0, 5.0, 310.0, height);
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //进入文件
    PolicyFileViewController *fileVC = [[PolicyFileViewController alloc]initWithNibName:@"PolicyFileViewController" bundle:nil];
    EnterprisePolicyFileItem *item =[_dataService.dataList objectAtIndex:indexPath.row];
    fileVC.contentID = item.Id;
    [self.navigationController pushViewController:fileVC animated:YES];
}
#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    [_tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}

//-(void)setEnableToLoadMore:(BOOL)enable
//{
//    self.ptr.enablePullToBottom = enable;
//}

#pragma mark - PullToRefresh Method
//捕获到拉动事件
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction
{
    NSLog(@"捕获到拉动事件");
    if (direction == MSRefreshDirectionTop) {
        [self.dataService loadDataWithKey:_searchKey];
    }
    if (direction == MSRefreshDirectionBottom) {
        //不允许下拉
//        [self.dataService loadMoreDataWithKey:_searchKey];
    }
}

#pragma mark - Search Bar Delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //    NSLog(@"search bar input:%@",searchText);
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search button clicked with input:%@",searchBar.text);
    [searchBar resignFirstResponder];
    if (searchBar.text.length == 0) {
        //如果没输入内容并且点击完成，则重新刷新列表
        [self startLoadingView];
        [self.dataService loadDataWithKey:nil];
    } else {
        //搜索框不为空，且不是快速搜索
        [self startLoadingView];
        [self.dataService loadDataWithKey:searchBar.text];
    }
}

- (void)didSearchText:(NSString *)text
{
    NSLog(@"search button clicked with input:%@",text);
//    [searchBar resignFirstResponder];
    if (text.length == 0) {
        //如果没输入内容并且点击完成，则重新刷新列表
        [self startLoadingView];
        [self.dataService loadDataWithKey:nil];
    } else {
        //搜索框不为空，且不是快速搜索
        [self startLoadingView];
        [self.dataService loadDataWithKey:text];
    }

}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"search bar end edit with input %@",searchBar.text);
    if (searchBar.text.length == 0 && self.searchKey != nil && !_onBackMethod) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
